package images

import (
	"context"

	"server/internal/common/biz"
	"server/internal/svc"
	"server/internal/types"

	"github.com/docker/docker/api/types/image"
	"github.com/zeromicro/go-zero/core/logx"
)

type RemoveLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// remove image
func NewRemoveLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveLogic {
	return &RemoveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RemoveLogic) Remove(req *types.RemoveImageReq) (resp bool, err error) {
	resps, err := l.svcCtx.DockerCli.ImageRemove(l.ctx, req.Id, image.RemoveOptions{
		Force: req.Force,
	})
	if err != nil {
		logx.Errorf("remove image failed, err: %v", err)
		return false, biz.ImageRemoveError
	}
	for _, resp := range resps {
		l.Logger.Infof("remove imageId: %s, status: %s", req.Id, resp)
	}

	return true, nil
}
